﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BtLabelDAL
{
    public class RfidDAL
    {
        private ConnectionConfig connection = null;
        public SqlSugarClient db = null;
        public RfidDAL()
        {
            connection = new ConnectionConfig()
            {
                ConnectionString = "Data Source=" + Environment.CurrentDirectory + "\\RfidDb.db",
                DbType = DbType.Sqlite,
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute
            };
            db = new SqlSugarClient(connection);
        }
        /// <summary>
        /// 查重mac
        /// </summary>
        /// <param name="addr">mac地址</param>
        /// <returns></returns>
        public bool CheckAddr(string addr)
        {
            try
            {
                int count = db.Queryable<BtLabelModel.RfidDbModel.Rfid_Table>().Where(it => it.mac == addr).Count();
                if (count != 0)
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="addr">mac地址</param>
        /// <returns></returns>
        public BtLabelModel.RfidDbModel.Rfid_Table GetModel(string addr)
        {
            try
            {
                return db.Queryable<BtLabelModel.RfidDbModel.Rfid_Table>().Where(it => it.mac == addr).First();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 上传数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Update(BtLabelModel.RfidDbModel.Rfid_Table model)
        {
            try
            {
                int count = db.Queryable<BtLabelModel.RfidDbModel.Rfid_Table>().Where(it => it.mac == model.mac).Count();
                int result = 0;
                if (count != 0)
                {
                    result = db.Updateable(model).Where(it => it.mac == model.mac).ExecuteCommand();
                }
                else
                {
                    result = db.Insertable(model).ExecuteCommand();
                }
                if (result == 0)
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="dateTime1">起始时间</param>
        /// <param name="dateTime2">结束时间</param>
        /// <returns></returns>
        public List<BtLabelModel.RfidDbModel.Rfid_Table> GetModelList(DateTime dateTime1, DateTime dateTime2)
        {
            try
            {
                return db.Queryable<BtLabelModel.RfidDbModel.Rfid_Table>().Where(it => it.time >= dateTime1 && it.time <= dateTime2).ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="dateTime1">起始时间</param>
        /// <param name="dateTime2">结束时间</param>
        /// <param name="mac">mac地址</param>
        /// <returns></returns>
        public List<BtLabelModel.RfidDbModel.Rfid_Table> GetModelList(DateTime dateTime1, DateTime dateTime2, string mac)
        {
            try
            {
                return db.Queryable<BtLabelModel.RfidDbModel.Rfid_Table>().Where(it => it.time >= dateTime1 && it.time <= dateTime2 && it.mac.Contains(mac)).ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
